/**
 * 年度费用预算维护作业
 * @author ganjia
 * @date 2019/10/25
 */
define(function () {

    var self,
        doc = document,
        app = win.app,
        layui = win.layui,
        element,
        table,
        table_param = {},
        cols = [[
            {type:'checkbox',fixed: 'left'},
            {field: 'GWFBNO', width: 80, title: '版本', fixed: 'left',sort: true,search:'default'},
            {field: 'GWFB001', width: 80, title: '年度', fixed: 'left',sort: true,searchOpts: {type: 'year'},search:'default'},
            {field: 'GWFB002', width: 80, title: '月份', fixed: 'left',sort: true,searchOpts: {type: 'MM'},search:'default'},
            {field: 'GWFB003', width: 80, title: '区域', sort: true,search:'default', searchOpts: {icon: true}},
            {field: 'GWFB003_NAME', width: 80, title: '区域名称'},
            {field: 'GWFB004', width: 120, title: '客户', sort: true,search:'default', searchOpts: {icon: true}},
            {field: 'GWFB004_NAME', width: 120, title: '客户名称'},
            {field: 'GWFB005', width: 80, title: '品类', sort: true,search:'default', searchOpts: {icon: true}},
            {field: 'GWFB005_NAME', width: 120, title: '品类名称'},
            {field: 'GWFB100', width: 80, title: '币别', sort: true},
            {field: 'GWFB100_NAME', width: 120, title: '名称'}
            , {field: 'GWFB101', width: 80, title: '汇率', sort: true}
            , {field: 'GWFB102', width: 160, title: '预测销售原币金额', sort: true}
            , {field: 'GWFB103', width: 160, title: '预测销售本币金额', sort: true}
            , {field: 'GWFB104', width: 100, title: '区域费率', sort: true}
            , {field: 'GWFB105', width: 100, title: '总部费率', sort: true}
            , {field: 'GWFBACTI', width: 100, title: '审核否',filterDatas: app.datas_('crm_gwei006_conf')},
            {
                width: 60,
                title: '',
                toolbar: '#script-gwft100setting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        gwft100SettingAction,
        editArea = ['600px', '400px']
    ;

    var gwft100 = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwft100',
                _searchs: [{
                    id: 'gwft100', tableId: 'gwft100_table', datas: cols[0]
                }],
                _loadTplsFull: [{
                    dom: 'gwft100_edit', tpl: 'gwft100_edit'
                },{
                    dom: 'common_open_window', tpl: 'common/_open_window'
                }]
            }, win);
        },

        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'gwft100_table'),
                    url: g_ctxPath + 'crm/gwft100/getGwfblist',
                    exportsUrl: g_ctxPath + 'crm/gwft100/getGwfblist',
                    exportsName: '年度费用预算数据',
                    title: '年度费用预算信息表',
                    method: 'post',
                    id: 'gwft100_table',
                    size: _layuiConfig.ui_size,
                    autoSort: false, //禁用前端自动排序
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter', 'exports'],
                    height: 'full',
                    cols: cols,
                    loading: true,
                    page: true,
                    where: '',
                    done: function (res, curr, count) {

                    }
                });

                layui.laytpl(app.getByid(doc, 'script-gwft100setting-action').innerHTML).render({}, function (string) {
                    gwft100SettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(gwft100_table)', function (obj) {

                    layer.tips(gwft100SettingAction, obj.td, {
                        tips: [3, '']
                        , time: 3000
                        , anim: 500
                        , maxWidth: 120
                        , skin: 'layui-table-tips'
                        , success: function (layero, index) {
                            layero.find('li').on('click', function () {
                                switch ($(this).attr('inx')) {
                                    case '0':
                                        self._openTplFull({
                                            tag: 'gwft100_edit',
                                            tagData: obj,
                                            index: 0,
                                            openOpts:{
                                                title: '年度费用预算信息编辑',
                                                maxmin: true
                                            }
                                        });
                                        break;
                                    case '1':
                                        self.delData(obj);
                                        break;
                                }
                                layer.close(index);
                            });
                        }
                    });
                });

                //监听行双击事件
                table.on('rowDouble(gwft100_table)', function (obj) {
                    self._openTplFull({
                        tag: 'gwft100_edit',
                        tagData: obj,
                        index: 0,
                        openOpts: {
                            title: '年度费用预算信息编辑',
                            maxmin: true
                        }
                    });
                });

                //监听事件
                table.on('toolbar(gwft100_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'gwft100_edit',
                                tagData: {
                                    is_add: true
                                },
                                index: 0,
                                openOpts: {
                                    title: '年度费用预算信息编辑',
                                    maxmin: true
                                }
                            });
                            break;
                        case 'del':
                            app.confirm({
                                msg: '是否删除选择的数据？',
                                ok: function () {
                                    self.delDataBatch();
                                }
                            });
                            break;
                        case 'check':
                            app.confirm({
                                msg: '是否审核选择的数据？',
                                ok: function () {
                                    self.check();
                                }
                            });
                            break;
                        case 'unCheck':
                            app.confirm({
                                msg: '是否取消审核选择的数据？',
                                ok: function () {
                                    self.unCheck();
                                }
                            });
                            break;
                    }
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },

        reloadTableCurr: function () {
            self._reloadTable({id: 'gwft100_table', where: table_param});
        },

        delData: function (obj) {
            app.confirm({
                msg: '是否删除选择的信息？',
                ok: function () {
                    app.ajax({
                        xurl: 'crm/gwft100/del',
                        xdata: obj.data,
                        loading: true,
                        okmsg: '年度费用预算删除成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                                // app.pageModule('gwai280').reloadTableCurr();
                                app.pageModule('gwft100').reloadTableCurr();
                            }
                        }
                    });
                }
            });

        },

        delDataBatch: function(){
            var checkStatus = table.checkStatus('gwft100_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('crm_gwft100');
                    _temp.GWFBNO = v.GWFBNO;
                    _temp.GWFBSEQ = v.GWFBSEQ;
                    _xdata.push(_temp);
                });
                app.ajax({
                    xurl: 'crm/gwft100/delDataBatch',
                    xdata: _xdata,
                    loading: true,
                    toJsonStr: true,
                    okmsg: '年度费用预算信息删除成功',
                    cb: function (result) {
                        if (result.ok) {
                            self.reloadTableCurr();
                            // app.pageModule('gwai280').reloadTableCurr();
                            app.pageModule('gwft100').reloadTableCurr();
                        }
                    }
                });
            }
        },
        check: function () {
            var flag = true;
            var checkStatus = table.checkStatus('gwft100_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('crm_gwft100');
                    if (v.GWFBACTI != 'N') {
                        app.showWarning("只有状态为N：未审核的资料才可以审核！");
                        flag = false;
                        return;
                    } else {
                        _temp.GWFBENT = v.GWFBENT;
                        _temp.GWFBNO = v.GWFBNO;
                        _temp.GWFBSEQ = v.GWFBSEQ;
                        _xdata.push(_temp);
                    }
                });
                if (flag) {
                    app.ajax({
                        xurl: 'crm/gwft100/check',
                        xdata: _xdata,
                        loading: true,
                        toJsonStr: true,
                        okmsg: '审核成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            }
        },
        unCheck: function () {
            var flag = true;
            var checkStatus = table.checkStatus('gwft100_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('crm_gwft100');
                    if (v.GWFBACTI != 'Y') {
                        app.showWarning("只有状态为Y：已审核的资料才可以取消审核！");
                        flag = false;
                        return;
                    } else {
                        _temp.GWFBENT = v.GWFBENT;
                        _temp.GWFBNO = v.GWFBNO;
                        _temp.GWFBSEQ = v.GWFBSEQ;
                        _xdata.push(_temp);
                    }
                });
                if (flag) {
                    app.ajax({
                        xurl: 'crm/gwft100/unCheck',
                        xdata: _xdata,
                        loading: true,
                        toJsonStr: true,
                        okmsg: '取消审核成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            }
        },

        _ready: function () {
            this._super({
                vue: [{
                    el: '#page',
                    data: '',
                    searchId: 'gwft100',
                    methods: {
                        handleIconClick: function (field) {
                            var vue = this;
                            if (field == 'GWFB003') {
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_gwad002',
                                            columnList: [
                                                {label: '区域编号', prop: 'GWADL002', width: '150'},
                                                {label: '区域名称', prop: 'GWADL004', width: '150'},
                                                {label: '助记码', prop: 'GWADL005', width: '150'}
                                            ],
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWFB003 = data.GWADL002;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择区域',
                                        area: ['600px', '465px']
                                    }
                                });
                            }else if (field == 'GWFB004') {
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_gwak001',
                                            columnList: [
                                                {label: '客户编号', prop: 'GWAK001', width: '150'},
                                                {label: '客户简称', prop: 'GWAKL003', width: '150'},
                                                {label: '客户全称', prop: 'GWAKL004', width: '150'}
                                            ],
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWFB004 = data.GWAK001;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择客户',
                                        area: ['600px', '465px']
                                    }
                                });
                            }else if (field == 'GWFB005') {
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_gwao001',
                                            columnList: [
                                                {label: '产品系列编号', prop: 'GWAO001', width: '150'},
                                                {label: '说明', prop: 'GWAOL003', width: '150'},
                                                {label: '助记码', prop: 'GWAOL004', width: '150'}
                                            ],
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWFB005 = data.GWAO001;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择品类',
                                        area: ['600px', '465px']
                                    }
                                });
                            }
                        }
                    }
                }]
            });
            self.initTable();
        },
        _lazy: function () {
            this._super();
            layui.use(['table'], function () {
                element = layui.element;
            });
        }
    });

    return new gwft100();

});
